<?php
/**
 * Created by JetBrains PhpStorm.
 * User: Jason
 * Date: 12-8-22
 * Time: 下午1:34
 * To change this template use File | Settings | File Templates.
 */

if (!defined('IN_ECS'))
{
    die('Hacking attempt');
}

/**
 * 取得某页的所有团购活动
 * @param   int     $size   每页记录数
 * @param   int     $page   当前页
 * @return  array
 */
function group_buy_list($size, $page)
{
    /* 取得团购活动 */
    $gb_list = array();
    $now = gmtime();
    $sql = "SELECT *, IFNULL(g.goods_thumb, '') AS goods_thumb, b.act_id AS group_buy_id, ".
        "b.start_time AS start_date, b.end_time AS end_date " .
        "FROM " . $GLOBALS['ecs']->table('goods_activity') . " AS b " .
        "LEFT JOIN " . $GLOBALS['ecs']->table('goods') . " AS g ON b.goods_id = g.goods_id " .
        "WHERE b.act_type = '" . GAT_GROUP_BUY . "' " .
        "AND b.start_time <= '$now' AND b.is_finished < 3 ORDER BY b.act_id DESC";
    $res = $GLOBALS['db']->selectLimit($sql, $size, ($page - 1) * $size);
    $i = 0;
    while ($group_buy = $GLOBALS['db']->fetchRow($res))
    {
        $ext_info = unserialize($group_buy['ext_info']);
        $group_buy = array_merge($group_buy, $ext_info);

        /* 格式化时间 */
        $group_buy['formated_start_date']   = local_date($GLOBALS['_CFG']['time_format'], $group_buy['start_date']);
        $group_buy['formated_end_date']     = local_date($GLOBALS['_CFG']['time_format'], $group_buy['end_date']);
        /* 格式化剩余时间 */
        $diff_time = time_diff(time(),$group_buy['end_date']);
        if($diff_time['day'] >=3 ){
            $group_buy['time_remain'] = '3天以上';
        } else {
            $group_buy['time_remain'] = $diff_time['day']."天".$diff_time['hour']."小时";
        }
        /* 查询销量 */
        $stat = group_buy_stat($group_buy['group_buy_id'], $group_buy['deposit']);
        //$group_buy = array_merge($group_buy, $stat);
        $group_buy['sale_count'] = $stat['total_order'];
        /* 获取留言  */
        $result = $GLOBALS['db']->query("select c.*,u.user_name from ".$GLOBALS['ecs']->table('comment')." c,".$GLOBALS['ecs']->table('users')." u where c.comment_type = 0 and c.id_value=".$group_buy['goods_id']." and c.status = 1 and c.user_id = u.user_id order by c.add_time desc limit 0,4");
        $ii = 0;
        $c_arr = array();
        while($r = $GLOBALS['db']->fetch_array($result)){
            $c_arr[$ii] = $r;
            $ii++;
        }
        $group_buy['comments'] = $c_arr;
        /* 格式化保证金 */
        $group_buy['formated_deposit'] = price_format($group_buy['deposit'], false);

        /* 处理价格阶梯 */
        $price_ladder = $group_buy['price_ladder'];
        if (!is_array($price_ladder) || empty($price_ladder))
        {
            $price_ladder = array(array('amount' => 0, 'price' => 0));
        }
        else
        {
            foreach ($price_ladder as $key => $amount_price)
            {
                $price_ladder[$key]['formated_price'] = price_format($amount_price['price']);
                $price_ladder[$key]['discount'] = round($amount_price['price'] * 10 / $group_buy['shop_price'],1);
            }
        }
        $group_buy['price_ladder'] = $price_ladder;
        $group_buy['class']        = $i%2 == 1?'tuan2':'tuan1';
        /* 处理图片 */
        if (empty($group_buy['goods_thumb']))
        {
            $group_buy['goods_thumb'] = get_image_path($group_buy['goods_id'], $group_buy['goods_thumb'], true);
        }
        /* 处理链接 */
        $group_buy['url'] = build_uri('group_buy', array('gbid'=>$group_buy['group_buy_id']));
        /* 加入数组 */
        $gb_list[] = $group_buy;
        $i++;
    }
    return $gb_list;
}

function time_diff($t1,$t2){
    $diff   = abs($t2 - $t1);
    $day    = floor((float)($diff / (3600 * 24)));
    $hour   = floor(($diff - $day * 3600 * 24) / 3600);
    $minute = floor(($diff - $day * 3600 * 24 - $hour * 3600) / 60);
    $second = floor($diff - $day * 3600 * 24 - $hour * 3600 - $minute * 60);
    return array("day"=>$day,"hour"=>$hour,"minute"=>$minute,"second"=>$second);
}

?>